package com.alibaba.demo.service.impl;

import com.alibaba.demo.entity.AyUser;
import com.alibaba.demo.repository.AyUserRepository;
import com.alibaba.demo.service.AyUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * @author zhouyanxiang
 * @create 2020-08-2020/8/24-15:06
 */
@Service
public class AyUserServiceImpl implements AyUserService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "ayUserRepository")
    private AyUserRepository ayUserRepository;

    @Resource
    private RedisTemplate redisTemplate;

    private static final String ALL_USER = "ALL_USER_LIST";

    @Override
    public AyUser findById(String id) {
        // step.1 查询Redis缓存中的所有数据
        List<AyUser> ayUserList = redisTemplate.opsForList().range(ALL_USER,0,-1);
        if (ayUserList != null && ayUserList.size() > 0){
            for (AyUser ayUser : ayUserList){
                if (ayUser.getId().equals(id)){
                    return ayUser;
                }
            }
        }
        // step.2 查询数据库的数据
        // java.util.NoSuchElementException: No value present
         AyUser ayUser = ayUserRepository.findById(id).get();
         if (ayUser != null){
             redisTemplate.opsForList().leftPush(ALL_USER,ayUser);
             return ayUser;
         }else {
             // 在数据库中也找不到
             logger.warn("在数据库中也找不到此数据");
             try {
                 throw new Exception("在数据库中也找不到此数据");
             } catch (Exception e) {
                 e.printStackTrace();
             }
             return null;
         }
/*        Optional<AyUser> ayUser = ayUserRepository.findById(id);
        if (ayUser != null && ayUser.isPresent()){
            ayUser = Optional.of(ayUserRepository.findById(id).get());
            // step.3 将数据都插入Redis缓存中
            redisTemplate.opsForList().leftPush(ALL_USER,ayUser);
            return ayUser.get();
        }else {
            return null;
        }*/


    }

    @Override
    public List<AyUser> findAll() {
        return ayUserRepository.findAll();
    }

    @Override
    public AyUser save(AyUser ayUser) {
        return ayUserRepository.save(ayUser);
    }

    @Override
    public void delete(String id) {
        ayUserRepository.deleteById(id);
    }

    @Override
    public Page<AyUser> findAll(Pageable pageable) {
        return ayUserRepository.findAll(pageable);
    }

    @Override
    public List<AyUser> findByName(String name) {
        return ayUserRepository.findByName(name);
    }

    @Override
    public List<AyUser> findByNameLike(String name) {
        return ayUserRepository.findByNameLike(name);
    }

    @Override
    public List<AyUser> findByIdIn(Collection<String> ids) {
        return ayUserRepository.findByIdIn(ids);
    }
}
